import { Video } from './data.d';
import { Effect } from 'dva';
import { Reducer } from 'umi';
import { getVideoList, updateVideo, deleteVideo, addVideo } from './service';


export interface ModelState {
    list: Video[];
    total: number;
    item: Video | {};
}
export type TEffect = Record<'fetchList' | 'updateOne' | 'deleteOne' | 'addOne', Effect>
export type TReducer = Record<'setList' | 'setItem', Reducer<ModelState>>

export default <ModelType<ModelState, TEffect, TReducer>>{
    namespace: 'video',
    state: {
        list: [],
        total: 0,
        item: {}
    },
    effects: {
        *fetchList({ payload }, { call, put }) {
            const res = yield call(getVideoList, payload);

            yield put({
                type: 'setList',
                payload: res.data
            })
        },
        *updateOne({ payload }, { call, put }) {
            return yield call(updateVideo, payload);
        },
        *deleteOne({ payload }, { call, put }) {
            return yield call(deleteVideo, payload);
        },
        *addOne({ payload }, { call, put }) {
            return yield call(addVideo, payload);
        }
    },
    reducers: {
        setList(state, { payload }) {
            return {
                ...state,
                list: [...payload.list],
                total: payload.count
            };
        },
        setItem(state, { payload }) {
            return {
                ...state,
                item: payload
            };
        }
    },

};
